package com.fy.service.imbusiness.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fy.common.constant.RedisKeyConstant;
import com.fy.common.dto.ChatSessionMuteDto;
import com.fy.common.dto.ChatSessionTopDto;
import com.fy.common.entity.SkImChatSession;
import com.fy.common.enums.SkImCommonStatusEnum;
import com.fy.common.util.TokenUtil;
import com.fy.framework.common.model.R;
import com.fy.framework.component.config.LocaleMessage;
import com.fy.framework.component.config.RedissonComponet;
import com.fy.service.service.SkImChatSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMapCache;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhshuo
 * @date 2021/12/15 10:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "会话列表")
@RequestMapping("/chatSession")
public class SkImChatSessionController {

    LocaleMessage localeMessage;

    RedissonComponet redissonComponet;

    SkImChatSessionService chatSessionService;

    @ApiOperation("查询会话列表")
    @PostMapping("list")
    public R<List<SkImChatSession>> list(){
       return chatSessionService.list();
    }

    @PostMapping("top")
    @ApiOperation("会话置顶")
    public R<?> top(@RequestBody ChatSessionTopDto sessionTopDto ){
       return chatSessionService.top(sessionTopDto);
    }

    @PostMapping("mute")
    @ApiOperation("会话免打扰")
    public R<?> mute(@RequestBody ChatSessionMuteDto sessionMuteDto){
        return chatSessionService.mute(sessionMuteDto);
    }

    /**
     * 删除会话
     * @return
     */
    @PostMapping("del")
    @ApiOperation("删除会话")
    @ApiImplicitParam(name = "chatSessionId", value = "cs-p-这种", example = "要被删除的会话ID")
    public R<?> del(@RequestBody JSONObject chatSession){
        try {
            chatSessionService.del(chatSession.getString("chatSessionId"));
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 清空聊天记录
     * @return
     */
    @PostMapping("clear")
    @ApiOperation("清空聊天记录")
    public R<?> clear(@RequestBody @ApiParam(name = "chatSessionId", value = "cs-p-这种", example = "要被删除的会话ID") JSONObject chatSession){
        try {
            chatSessionService.clear(chatSession.getString("chatSessionId"));
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failedAlter(localeMessage.getMessage("im.common.failedful"));
        }
    }

    /**
     * 标记未读
     * @param param
     * @return
     */
    @ApiOperation("标记未读")
    @PostMapping("setUnread")
    public R<?> setUnread(@RequestBody @ApiParam(name = "chatSessionId", value = "会话ID") JSONObject param){
        RMapCache<String, Object> map = redissonComponet.getMap(RedisKeyConstant.USER_CHAT_SESSION + TokenUtil.getUserId());
        String chatSession = param.getString("chatSessionId");
        if(map.containsKey(chatSession)){
            SkImChatSession session = (SkImChatSession)map.get(chatSession);
            session.setUnreadCount(1);
            map.put(chatSession,session);
            return R.success(localeMessage.getMessage("im.common.successful"));
        }
        return R.failedAlter(localeMessage.getMessage("im.common.parameterError"));
    }

    /**
     * 获取目标对象的会话
     * @param param
     * @return
     */
    @ApiOperation("获取目标对象的会话")
    @PostMapping("chatWithTarget")
    public R<SkImChatSession> chatWithTarget(@RequestBody @ApiParam(name = "targetId", value = "会话ID") JSONObject param){
        SkImChatSession session = chatSessionService.selectOne(new EntityWrapper<SkImChatSession>().eq("sk_im_user_id", TokenUtil.getUserId()).eq("target_id", param.getLong("targetId"))
                .ne("status", SkImCommonStatusEnum.DELETED.getStatus()));
        return R.success(session);
    }

}
